package com.handu.luna.core.domain.promotion.combo;

import com.handu.luna.core.domain.BasePersistentDomain;
import com.handu.luna.core.domain.BossException;
import com.handu.luna.core.domain.goods.Goods;
import com.handu.luna.core.domain.promotion.Promotion.PromotionStatus;
import com.handu.luna.core.domain.search.ComboSearch;

import org.apache.commons.lang.StringUtils;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.format.annotation.DateTimeFormat;

import javax.persistence.Entity;
import javax.persistence.Lob;
import javax.persistence.Transient;
import javax.persistence.criteria.*;
import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import javax.validation.constraints.Size;

import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * Created by zhq on 2014/7/23.
 */
@Entity
public class Combo extends BasePersistentDomain<ComboRepository> {

    @Size(max = 100,message = "套餐名称不可超过100字")
    private String name;
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private Date startDate;
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private Date endDate;

    @Max(value = 254,message = "显示顺序最大为254")
    @Min(value = 0 ,message = "显示顺序最小为0")
    private int displayOrder = 100;


    private BigDecimal price;

    @Lob
    private String description;

    private String comboGoodsIds;

    @Transient
    private List<Goods> comboGoods;
    
    @Transient
    private String status;
    

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Date getStartDate() {
        return startDate;
    }

    public void setStartDate(Date startDate){
        this.startDate = startDate;

    }

    public Date getEndDate() {
        return endDate;
    }

    public void setEndDate(Date endDate) {
        this.endDate = endDate;
    }

    public int getDisplayOrder() {
        return displayOrder;
    }

    public void setDisplayOrder(int displayOrder) {
        this.displayOrder = displayOrder;
    }

    public BigDecimal getPrice() {
        return price;
    }

    public void setPrice(BigDecimal price) {
        this.price = price;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public String getComboGoodsIds() {
        return comboGoodsIds;
    }

    public void setComboGoodsIds(String comboGoodsIds) {
        this.comboGoodsIds = comboGoodsIds;
    }

    public List<Goods> getComboGoods() {
        if (StringUtils.isNotBlank(comboGoodsIds)){
            comboGoods = context.getGoodsRepository().findAll(Goods.in(comboGoodsIds));
        }
        return comboGoods;
    }

    public void setComboGoods(List<Goods> comboGoods) {
        this.comboGoods = comboGoods;
    }
    
    

    public String getStatus() {
		return initStatus(this.getStartDate(), this.getEndDate());
	}

	public void setStatus(String status) {
		this.status = status;
	}
	
	public static String initStatus(Date startDate,Date endDate) {
        Date now = new Date();
        if(startDate != null && endDate != null) {
            if(now.compareTo(startDate)==-1){
			return  "<font color='#006400'> 【"+PromotionStatus.UNACTIVATED.description+"】</font>";
            }else if(endDate.compareTo(now)==-1){
            	return "<font color='#696969'>【"+PromotionStatus.ACTIVATED.description+"】</font>";
            }else{
            	return "<font color='#8B0000'>【"+PromotionStatus.ACTIVATING.description+"】</font>";
            }
        }else {
        	return "";
        }

    }

	public static Specification<Combo> complexQuery(ComboSearch search){
        return new Specification<Combo>() {
            @Override
            public Predicate toPredicate(Root<Combo> root, CriteriaQuery<?> query, CriteriaBuilder cb) {

                Predicate predicate = cb.conjunction();

                List<Expression<Boolean>> expressions = predicate.getExpressions();
                DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                if (StringUtils.isNotBlank(search.getComboName())){
                    expressions.add(cb.like(root.<String>get("name"),'%'+search.getComboName()+"%"));
                }

                if (StringUtils.isNotBlank(search.getComboStartDate())){
                    try{
                        expressions.add(cb.greaterThanOrEqualTo(root.<Date>get("startDate"), df.parse(search.getComboStartDate())));
                    }catch (ParseException e){
                        throw new BossException("日期转换异常");
                    }
                }

                if (StringUtils.isNotBlank(search.getComboEndDate())){
                    try{
                        expressions.add(cb.lessThanOrEqualTo(root.<Date>get("startDate"),df.parse(search.getComboEndDate())));
                    }catch (ParseException e){
                        throw new BossException("日期转换异常");
                    }
                }
                
                if(search.getStatus().equals(PromotionStatus.ACTIVATED.name())){
                	try{
                        expressions.add(cb.lessThanOrEqualTo(root.<Date>get("endDate"),new Date()));
                    }catch (Exception e){
                        throw new BossException("日期转换异常");
                    }
                }else if(search.getStatus().equals(PromotionStatus.UNACTIVATED.name())){
                	try{
                        expressions.add(cb.greaterThanOrEqualTo(root.<Date>get("startDate"),new Date()));
                    }catch (Exception e){
                        throw new BossException("日期转换异常");
                    }
                }else if(search.getStatus().equals(PromotionStatus.ACTIVATING.name())){
                	try{
                        expressions.add(cb.lessThanOrEqualTo(root.<Date>get("startDate"),new Date()));
                        expressions.add(cb.greaterThanOrEqualTo(root.<Date>get("endDate"),new Date()));
                	}catch (Exception e){
                        throw new BossException("日期转换异常");
                    }
                }
                

                return query.where(predicate).getRestriction();
            }
        };
    }

    public Combo(String name, Date startDate, Date endDate, BigDecimal price, String comboGoodsIds) {
        this.name = name;
        this.startDate = startDate;
        this.endDate = endDate;
        this.price = price;
        this.comboGoodsIds = comboGoodsIds;
    }

    public Combo(){super();}
}
